package com.jl.tools.ui.dialog;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.ResourceManager;

import com.jl.util.Calculator;
import com.jl.util.DecUtil;
import com.jl.util.SWTDialogUtil;
import com.jl.util.HexUtil;
import com.jl.util.LengthUtil;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.ModifyEvent;

public class CalculatorDialog extends Dialog {

	protected Object result;
	protected Shell shlSimpleCalculator;
	private Text textDec;
	private Text textHex;
	private Text textReplace;
	private Label lblP;
	private Label lblP_1;
	private Text textP1;
	private Text textP2;
	private Label lblRes;
	private Text textRes;
	private Composite composite_1;
	private Button btnXor;
	private Button btnOr;
	private Button btnAnd;
	private Button btnRsh;
	private Button btnLsh;
	private Button btnRol;
	private Button btnRor;
	private Button btnMod;
	private Button btnAdd;
	private Button btnSub;
	private Button btnMul;
	private Button btnDiv;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public CalculatorDialog(Shell parent, int style) {
		super(parent, style);
		setText("Calculator Dialog");
		createContents();
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {

		Display display = getParent().getDisplay();
		SWTDialogUtil.locateToParentCenter(getParent(), shlSimpleCalculator);
		shlSimpleCalculator.open();
		shlSimpleCalculator.layout();
		while (!shlSimpleCalculator.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shlSimpleCalculator = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.MIN);
		shlSimpleCalculator.setImage(ResourceManager.getPluginImage("com.jl.smartcard.tools", "icons/calculator_16.png"));
		shlSimpleCalculator.setSize(529, 330);
		shlSimpleCalculator.setText("Simple Calculator");
		shlSimpleCalculator.setLayout(new FormLayout());

		Composite composite = new Composite(shlSimpleCalculator, SWT.NONE);
		composite.setLayout(new GridLayout(4, false));
		FormData fd_composite = new FormData();
		fd_composite.top = new FormAttachment(0, 10);
		fd_composite.left = new FormAttachment(0, 10);
		fd_composite.right = new FormAttachment(100, -10);
		fd_composite.bottom = new FormAttachment(100, -10);
		composite.setLayoutData(fd_composite);

		lblP = new Label(composite, SWT.NONE);
		lblP.setFont(SWTResourceManager.getFont("Courier New", 11, SWT.NORMAL));
		lblP.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1,
				1));
		lblP.setText("P_1");

		textP1 = new Text(composite, SWT.BORDER);
		textP1.setFont(SWTResourceManager
				.getFont("Courier New", 11, SWT.NORMAL));
		textP1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3,
				1));

		lblP_1 = new Label(composite, SWT.NONE);
		lblP_1.setFont(SWTResourceManager
				.getFont("Courier New", 11, SWT.NORMAL));
		lblP_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		lblP_1.setText("P_2");

		textP2 = new Text(composite, SWT.BORDER);
		textP2.setFont(SWTResourceManager
				.getFont("Courier New", 11, SWT.NORMAL));
		textP2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3,
				1));
		new Label(composite, SWT.NONE);

		composite_1 = new Composite(composite, SWT.NONE);
		GridData gd_composite_1 = new GridData(SWT.FILL, SWT.FILL, false,
				false, 3, 1);
		gd_composite_1.heightHint = 50;
		gd_composite_1.widthHint = 114;
		composite_1.setLayoutData(gd_composite_1);

		btnXor = new Button(composite_1, SWT.NONE);
		btnXor.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				event_click_btn_operation(e);
			}
		});
		btnXor.setFont(SWTResourceManager
				.getFont("Courier New", 11, SWT.NORMAL));
		btnXor.setBounds(0, 0, 72, 22);
		btnXor.setText("XOR^");

		btnOr = new Button(composite_1, SWT.NONE);
		btnOr.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				event_click_btn_operation(e);
			}
		});
		btnOr.setFont(SWTResourceManager.getFont("Courier New", 11, SWT.NORMAL));
		btnOr.setBounds(78, 0, 72, 22);
		btnOr.setText("OR|");

		btnAnd = new Button(composite_1, SWT.NONE);
		btnAnd.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				event_click_btn_operation(e);
			}
		});
		btnAnd.setFont(SWTResourceManager
				.getFont("Courier New", 11, SWT.NORMAL));
		btnAnd.setBounds(156, 0, 72, 22);
		btnAnd.setText("AND&&");

		btnRsh = new Button(composite_1, SWT.NONE);
		btnRsh.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				event_click_btn_operation(e);
			}
		});
		btnRsh.setFont(SWTResourceManager
				.getFont("Courier New", 11, SWT.NORMAL));
		btnRsh.setBounds(78, 28, 72, 22);
		btnRsh.setText("RSH<<");

		btnLsh = new Button(composite_1, SWT.NONE);
		btnLsh.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				event_click_btn_operation(e);
			}
		});
		btnLsh.setFont(SWTResourceManager
				.getFont("Courier New", 11, SWT.NORMAL));
		btnLsh.setBounds(0, 28, 72, 22);
		btnLsh.setText("LSH>>>");

		btnRol = new Button(composite_1, SWT.NONE);
		btnRol.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				event_click_btn_operation(e);
			}
		});
		btnRol.setFont(SWTResourceManager
				.getFont("Courier New", 11, SWT.NORMAL));
		btnRol.setBounds(156, 28, 72, 22);
		btnRol.setText("ROL");

		btnRor = new Button(composite_1, SWT.NONE);
		btnRor.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				event_click_btn_operation(e);
			}
		});
		btnRor.setFont(SWTResourceManager
				.getFont("Courier New", 11, SWT.NORMAL));
		btnRor.setBounds(234, 28, 72, 22);
		btnRor.setText("ROR");

		btnMod = new Button(composite_1, SWT.NONE);
		btnMod.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				event_click_btn_operation(e);
			}
		});
		btnMod.setFont(SWTResourceManager
				.getFont("Courier New", 11, SWT.NORMAL));
		btnMod.setBounds(234, 0, 72, 22);
		btnMod.setText("MOD%");

		btnAdd = new Button(composite_1, SWT.NONE);
		btnAdd.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				event_click_btn_operation(e);
			}
		});
		btnAdd.setFont(SWTResourceManager
				.getFont("Courier New", 11, SWT.NORMAL));
		btnAdd.setBounds(312, 0, 72, 22);
		btnAdd.setText("+");

		btnSub = new Button(composite_1, SWT.NONE);
		btnSub.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				event_click_btn_operation(e);
			}
		});
		btnSub.setFont(SWTResourceManager
				.getFont("Courier New", 11, SWT.NORMAL));
		btnSub.setBounds(389, 0, 72, 22);
		btnSub.setText("-");

		btnMul = new Button(composite_1, SWT.NONE);
		btnMul.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				event_click_btn_operation(e);
			}
		});
		btnMul.setFont(SWTResourceManager
				.getFont("Courier New", 11, SWT.NORMAL));
		btnMul.setBounds(312, 28, 72, 22);
		btnMul.setText("*");

		btnDiv = new Button(composite_1, SWT.NONE);
		btnDiv.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				event_click_btn_operation(e);
			}
		});
		btnDiv.setFont(SWTResourceManager
				.getFont("Courier New", 11, SWT.NORMAL));
		btnDiv.setBounds(389, 28, 72, 22);
		btnDiv.setText("/");

		lblRes = new Label(composite, SWT.NONE);
		lblRes.setFont(SWTResourceManager
				.getFont("Courier New", 11, SWT.NORMAL));
		lblRes.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		lblRes.setText("Res");

		textRes = new Text(composite, SWT.BORDER);
		textRes.setFont(SWTResourceManager.getFont("Courier New", 11,
				SWT.NORMAL));
		textRes.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				3, 1));

		Label label = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 4,
				1));

		Label lblDecimal = new Label(composite, SWT.NONE);
		lblDecimal.setFont(SWTResourceManager.getFont("Courier New", 11,
				SWT.NORMAL));
		lblDecimal.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblDecimal.setText("Dec");

		textDec = new Text(composite, SWT.BORDER);
		textDec.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				event_modify_text_dec(e);
			}
		});
		textDec.setFont(SWTResourceManager.getFont("Courier New", 11,
				SWT.NORMAL));
		textDec.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label lblHex = new Label(composite, SWT.NONE);
		lblHex.setFont(SWTResourceManager
				.getFont("Courier New", 11, SWT.NORMAL));
		lblHex.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		lblHex.setText("Hec");

		textHex = new Text(composite, SWT.BORDER);
		textHex.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				event_modify_text_hex(e);
			}
		});
		textHex.setFont(SWTResourceManager.getFont("Courier New", 11,
				SWT.NORMAL));
		textHex.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Button btnR = new Button(composite, SWT.NONE);
		btnR.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				event_click_btn_replace();
			}
		});
		btnR.setToolTipText("Replace");
		btnR.setFont(SWTResourceManager.getFont("Courier New", 11, SWT.NORMAL));
		GridData gd_btnR = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
		gd_btnR.widthHint = 25;
		gd_btnR.heightHint = 61;
		btnR.setLayoutData(gd_btnR);
		btnR.setText("R");

		textReplace = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
		textReplace.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				event_mofigy_text_replace();
				
			}
		});
		textReplace.setFont(SWTResourceManager.getFont("Courier New", 11,
				SWT.NORMAL));
		textReplace.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true,
				3, 1));
	}

	protected void event_mofigy_text_replace() {
		int length = HexUtil.getHexCount(this.textReplace.getText());
		this.textDec.setText(length + "");
		this.textHex.setText(Integer.toHexString(length).toUpperCase());
	}

	protected void event_click_btn_operation(SelectionEvent e) {
		byte[] p1 = HexUtil.parseHexToByteArray(this.textP1.getText());
		String p2Text = this.textP2.getText().trim();
		if (p2Text == null || p2Text.equals("")) {
			p2Text = "00";
		}
		byte[] res = null;
		Object source = e.getSource();
		if (source == this.btnLsh) {
			res = Calculator.lsh(p1, Integer.parseInt(p2Text, 10));
		} else if (source == this.btnRsh) {
			res = Calculator.rsh(p1, Integer.parseInt(p2Text, 10));
		} else if (source == this.btnRol) {
			res = Calculator.rol(p1, Integer.parseInt(p2Text, 10));
		} else if (source == this.btnRor) {
			res = Calculator.ror(p1, Integer.parseInt(p2Text, 10));
		} else {
			byte[] p2 = HexUtil.parseHexToByteArray(this.textP2.getText());
			if (source == this.btnXor) {
				res = Calculator.xor(p1, p2);
			} else if (source == this.btnAnd) {
				res = Calculator.and(p1, p2);
			} else if (source == this.btnOr) {
				res = Calculator.or(p1, p2);
			} else if (source == this.btnAdd) {
				res = Calculator.add(p1, p2);
			} else if (source == this.btnSub) {
				res = Calculator.sub(p1, p2);
			} else if (source == this.btnMul) {
				res = Calculator.mul(p1, p2);
			} else if (source == this.btnDiv) {
				res = Calculator.div(p1, p2);
			} else if (source == this.btnMod) {
				res = Calculator.mod(p1, p2);
			}
		}
		if (res != null) {
			this.textRes.setText(HexUtil.toHexString(res));
		}
	}

	protected void event_modify_text_hex(ModifyEvent e) {
		if (this.textHex.isFocusControl()) {
			String text = this.textHex.getText();
			if (!text.trim().equals("")) {
				text = HexUtil.toDecimalString(text);
			}
			this.textDec.setText(text);
		}
	}

	protected void event_modify_text_dec(ModifyEvent e) {
		if (this.textDec.isFocusControl()) {
			String text = this.textDec.getText();
			if (!text.trim().equals("")) {
				text = DecUtil.toHexString(text);
			}
			this.textHex.setText(text);
		}
	}

	protected void event_click_btn_replace() {
		String text = this.textReplace.getText();
		text = LengthUtil.replace(text);
		this.textReplace.setText(HexUtil.format(text));
		int count = HexUtil.getHexCount(text);
		this.textDec.setText(count + "");
		this.textHex.setText(Integer.toHexString(count).toUpperCase());
		this.textReplace.setFocus();
		this.textReplace.selectAll();
	}
}
